package com.fc.framework.action;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.fc.framework.BusinessException;
import com.fc.framework.core.util.RequestUtil;

public abstract class AbstractAction {

    private static final Logger log = LoggerFactory.getLogger(AbstractAction.class);

    public static final String PARAM_PAGE_INDEX = "page";

    public static final String PARAM_PAGE_SIZE = "size";

    public static Integer DEFAULT_PAGE_INDEX = 1;

    public static Integer DEFAULT_PAGE_SIZE = 10;

    public static final String SESSION_LAST_URL = "LAST_URL";
    public static final String SESSION_LAST_METHOD = "LAST_METHOD";

    public static final String MODEL_RESULT = "operationResult";

    protected Integer getPageIndex(HttpServletRequest request) {
        Integer pageIndex = RequestUtil.getParamIntValue(request, PARAM_PAGE_INDEX);
        return pageIndex == null ? DEFAULT_PAGE_INDEX : pageIndex;
    }

    protected Integer getPageSize(HttpServletRequest request) {
        Integer pageSize = RequestUtil.getParamIntValue(request, PARAM_PAGE_SIZE);
        return pageSize == null ? DEFAULT_PAGE_SIZE : pageSize;
    }

    public String forward(String url) {
        return "forward:" + url;
    }

    public String redirect(String url) {
        return "redirect:" + url;
    }

    @SuppressWarnings("unchecked")
    protected void remberParams(HttpServletRequest request) {
        String URL = request.getServletPath();

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            if (URL.indexOf("?") > 0) {
                URL = URL + "&";
            } else {
                URL = URL + "?";
            }
            URL = URL + parameterName + "=" + request.getParameter(parameterName);
        }

        RequestUtil.setSessionAttribute(request, SESSION_LAST_URL, URL);
        RequestUtil.setSessionAttribute(request, SESSION_LAST_METHOD, request.getMethod());
    }

    protected String redirectToLastPage(HttpServletRequest request, Model model) {
        model.addAttribute("url", RequestUtil.getSessionAttribute(request, SESSION_LAST_URL));
        model.addAttribute("method", RequestUtil.getSessionAttribute(request, SESSION_LAST_METHOD));
        return "redirect";
    }

    protected String failed(Model model, String url, Exception e) {
        OperationResult operationResult = new OperationResult();
        operationResult.setResultType(ResultType.DANGER);
        operationResult.setMessage(e.getMessage());

        model.addAttribute(MODEL_RESULT, operationResult);

        return url;
    }

    protected String success(Model model, String url) {
        OperationResult operationResult = new OperationResult();
        operationResult.setMessage("操作成功!!!");

        model.addAttribute(MODEL_RESULT, operationResult);

        return url;
    }

    protected OperationResult failed(String errorMsg) {
        OperationResult operationResult = new OperationResult();
        operationResult.setResultType(ResultType.DANGER);
        operationResult.setMessage(errorMsg);
        return operationResult;
    }
    
    protected OperationResult failed(Exception e) {
        OperationResult operationResult = new OperationResult();
        operationResult.setResultType(ResultType.DANGER);
        operationResult.setMessage(e.getMessage());
        return operationResult;
    }

    protected OperationResult success() {
        OperationResult operationResult = new OperationResult();
        operationResult.setMessage("操作成功!!!");
        return operationResult;
    }
    

    public static enum ResultType {
        SUCCESS, INFO, WARNING, DANGER;
    }

    public static class OperationResult {

        private ResultType resultType = ResultType.SUCCESS;

        private String message = "";

        private Object data = "";

        public OperationResult() {
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public ResultType getResultType() {
            return resultType;
        }

        public void setResultType(ResultType resultType) {
            this.resultType = resultType;
        }
    }

    @SuppressWarnings("unchecked")
    @ExceptionHandler(BusinessException.class)
    public String handleException(BusinessException ex, HttpServletRequest request) {
        log.error(ex.getMessage(), ex);

        Enumeration<String> e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String parameterName = e.nextElement();
            request.setAttribute(parameterName, request.getParameter(parameterName));
        }

        OperationResult operationResult = new OperationResult();
        operationResult.setResultType(ResultType.DANGER);
        operationResult.setMessage(ex.getMessage());

        request.setAttribute(MODEL_RESULT, operationResult);

        return request.getServletPath();
    }
}
